package com.lagou.niancheng.rpc.client.netty.handler;

import com.lagou.niancheng.rpc.client.netty.RequestResultRegistry;
import com.lagou.niancheng.rpc.common.enums.SignalEnum;
import com.lagou.niancheng.rpc.common.handler.RequestHandler;
import io.netty.channel.Channel;

/**
 * 客户端请求结果处理器
 *
 * @author 大犬
 * @date 2020/5/1
 */
public class BaseRequestHandler<T> implements RequestHandler<T> {

    private RequestResultRegistry requestResultRegistry;

    public BaseRequestHandler(RequestResultRegistry requestResultRegistry) {
        this.requestResultRegistry = requestResultRegistry;
    }

    @Override
    public Object handle(T data) {

        return SignalEnum.FINISH_CONNECTION;
    }

    @Override
    public Object handle(T data, Channel channel) {
        // 将结果注册到注册中心中,并唤醒等待的线程
        requestResultRegistry.registerResultAndNotify(channel, data);
        return SignalEnum.FINISH_CONNECTION;
    }
}
